#include <stdlib.h>
#include <assert.h>
#include "Log.h"
#include "math.h"
#include "test.h"

JNIEXPORT jint JNICALL JNI_Add(JNIEnv* env, jobject thiz, jint x, jint y) {
	return add(x, y);
}

JNIEXPORT jint JNICALL JNI_Minus(JNIEnv* env, jobject thiz, jint x, jint y) {
	return minus(x, y);
}

static JNINativeMethod gMethods[] = {
		/* name,						signature,						funcPtr */
		{ "native_Add", 				"(II)I", 							(void*) JNI_Add },
		{ "native_Minus", 			"(II)I", 							(void*) JNI_Minus },
};

int registerNativeMethods(JNIEnv * env) {
	LOGD("registerNativeMethods <----");

	jclass clazz = env->FindClass(CLASS_NAME);

	if (clazz == NULL) {
		LOGE("clazz is null");
		return JNI_FALSE;
	}

	if (env->RegisterNatives(clazz, gMethods, sizeof(gMethods) / sizeof(JNINativeMethod)) < 0) {
		LOGE("RegisterNatives failed");
		return JNI_FALSE;
	}

	LOGD("registerNativeMethods ---->");
	return JNI_TRUE;
}

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
	LOGD("JNI_OnLoad <---");

	JNIEnv* env = NULL;
	jint result = -1;

	if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
		LOGE("JNI_VERSION_1_4 is not OK !!! ");
		return result;
	}

	assert(env != NULL);

	if (!registerNativeMethods(env)) {
		LOGE("registerNativeMethods failed");
		return result;
	}

	/* success -- return valid version number */
	result = JNI_VERSION_1_4;

	LOGD("JNI_OnLoad --->");
	return result;
}
